package com.itheima.mongo;

import cn.hutool.core.convert.Convert;
import com.itheima.dto.mongo.RecommendDto;
import com.itheima.mongoservice.RecommendServiceApi;
import com.itheima.pojo.mongo.RecommendUser;
import com.itheima.vo.RecommendFriend;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Slf4j
@DubboService
public class RecommendServiceApiImpl implements RecommendServiceApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public RecommendUser todayBest(Long toUserId) {
        /*//创建条件查询
        Query query = new Query(Criteria.where("toUserId").is(toUserId));
        //进行排序及限制数量
        Query score = query.with(Sort.by(Sort.Order.desc("score"))).limit(1);*/
        Query query = Query.query(Criteria.where("toUserId").is(toUserId))
                .with(Sort.by(Sort.Order.desc("score"))) //按照缘分值倒序排列
                .limit(1);//取第一条
        //开始查询结果(注意使用findone)
        /*RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        //返回
        return recommendUser;*/

        return mongoTemplate.findOne(query, RecommendUser.class);

    }

    /**
     * 原因:需要获取与id对应的匹配的人
     *
     * @return
     */
    public List<RecommendUser> getRecommendFriend(Long toUserId, RecommendDto recommendDto) {
        //开始查找匹配值近似的(这里对应的是toUserId)
        //开启条件查询
        Query query = new Query(Criteria.where("toUserId")
                .is(toUserId))
                .skip((recommendDto.getPage() - 1) * recommendDto.getPagesize())
                .limit(recommendDto.getPagesize())
                .with(Sort.by(Sort.Order.desc("score")));
        //分页查询
        return mongoTemplate.find(query, RecommendUser.class);
    }


    public RecommendFriend getRecommendFriendPageNum(Long toUserId, RecommendDto recommendDto) {
        RecommendFriend recommendFriend = new RecommendFriend();
        //开始查找匹配值近似的(这里对应的是toUserId)
        //开启条件查询
        Query query = new Query(Criteria.where("toUserId")
                .is(toUserId));
                /*.skip((recommendDto.getPage()-1 )*recommendDto.getPagesize())
//                .limit(recommendDto.getPagesize())
                .with(Sort.by(Sort.Order.desc("score")));*/
        //分页查询
//        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        //直接把总数写出来
        Integer counts = Convert.toInt(mongoTemplate.count(query, RecommendUser.class));
        recommendFriend.setCounts(counts);
        //计算分页总数量
        int pages1 = counts / recommendDto.getPagesize();
        int pages2 = counts % recommendDto.getPagesize();
        if (pages2 != 0) {
            pages1 = pages1 + 1;
        }
        //将计算出的条目数封装到实体中
        recommendFriend.setPages(pages1);
        log.info("{}", recommendFriend);
        return recommendFriend;
    }

    @Override
    public List<RecommendUser> findByToUid(Long uid) {
        Query query = Query.query(Criteria.where("toUserId").is(uid))
                .with(Sort.by(Sort.Order.desc("score"))); //按照缘分值倒序排列
        return mongoTemplate.find(query, RecommendUser.class);
    }

    //查询用户推荐
    @Override
    public RecommendUser findPageList(Long uid, Long friendId) {
        //构建条件查询
        Query query = Query.query(Criteria.where("userId").is(friendId).and("toUserId").is(uid));
//        Query query = new Query(Criteria.where("userId").is(uid));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        return recommendUser;
    }

}
